package com.sam.jifenbao;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sam.jifenbao.dialog.RedeemDialog;

public class CouponDetailActivity extends BaseActivity {
    private TextView couponValueTV,couponNameTV,couponRuleTV,couponValidTimeTV;
    private View suiteForShopBtn,couponDetailPanel;
    private Button useBtn;
    private RedeemDialog verifyDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        log("onCreate() 被调用");
        setContentView(R.layout.activity_coupon_detail);
        findView();
        initView();
    }

    @Override
    protected void findView() {
        couponValueTV = (TextView) findViewById(R.id.coupon_value);
        couponNameTV = (TextView) findViewById(R.id.coupon_name);
        couponRuleTV = (TextView) findViewById(R.id.coupon_rule);
        couponValidTimeTV  = (TextView) findViewById(R.id.coupon_valid_time);
        useBtn = (Button) findViewById(R.id.use_btn);
        suiteForShopBtn = findViewById(R.id.suite_for_shop_btn);
        couponDetailPanel = findViewById(R.id.coupon_detail_panel);
    }

    @Override
    protected void initView() {
        initDialog();
        useBtn.setOnClickListener(onClickListener);
        suiteForShopBtn.setOnClickListener(onClickListener);

        Intent intent = getIntent();
        if(intent == null) return;
        try {
//            couponValueTV.setText(intent.getStringExtra(CouponListActivity.KEY_COUPON_VALUE));
//            couponNameTV.setText(intent.getStringExtra(CouponListActivity.KEY_COUPON_NAME));
//            couponRuleTV.setText(intent.getStringExtra(CouponListActivity.KEY_COUPON_RULE));
//            couponValidTimeTV.setText(intent.getStringExtra(CouponListActivity.KEY_COUPON_VALID_TIME));
        } catch (Exception e){
            Log.e("sam","在优惠券详情页面获取传过来的信息出错");
        }

        couponDetailPanel.setEnabled(true);
        useBtn.setText(getString(R.string.use_right_now));
        useBtn.setEnabled(true);
    }

    private void initDialog(){
        verifyDialog = new RedeemDialog(this);
        verifyDialog.setOnCouponUseSccueededLinstener(onCouponUseSccueededLinstener);
    }

    private RedeemDialog.OnCouponUseSccueededLinstener onCouponUseSccueededLinstener = new RedeemDialog.OnCouponUseSccueededLinstener() {
        @Override
        public void onCouponUseSeccueeded() {
            couponDetailPanel.setEnabled(false);
            useBtn.setText(getString(R.string.already_used));
            useBtn.setEnabled(false);
        }
    };

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.suite_for_shop_btn:
                    startActivity(new Intent(CouponDetailActivity.this,ShopListActivity.class));
                    break;
                case R.id.use_btn:
                    verifyDialog.show();
                    break;
            }
        }
    };
}
